package cn.jbooter.session.autoconfigure.properties;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.session.data.redis.RedisFlushMode;

import cn.jbooter.session.autoconfigure.enu.StoreType;

/**
 * spring-session配置文件
 * @author hejian
 *
 */
@ConfigurationProperties(prefix = "spring.jsession")
public class ISpringSessionProperties {

	/**
	 * 是否启用
	 */
	private boolean enabled;
	
	/**
	 * session存储类型,默认使用hash_map
	 */
	private StoreType storeType=StoreType.HASH_MAP;
	
	/**
	 * session超时时间;单秒:秒
	 */
	private Integer timeout;
	
	/** 是否启用restfull接口的session支持(header:x-auth-token) **/
	private boolean restStrategy;
	
	/**
	 * session的redis配置
	 */
	private Redis redis = new Redis();
	
	public ISpringSessionProperties(ObjectProvider<ServerProperties> serverProperties) {
		ServerProperties properties = serverProperties.getIfUnique();
		this.timeout = (properties != null ? properties.getSession().getTimeout() : null);
	}
	
	public Integer getTimeout() {
		return timeout;
	}
	public void setTimeout(Integer timeout) {
		this.timeout = timeout;
	}
	public Redis getRedis() {
		return redis;
	}
	public void setRedis(Redis redis) {
		this.redis = redis;
	}
	public boolean isEnabled() {
		return enabled;
	}
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}
	public boolean isRestStrategy() {
		return restStrategy;
	}
	public void setRestStrategy(boolean restStrategy) {
		this.restStrategy = restStrategy;
	}
	public StoreType getStoreType() {
		return storeType;
	}
	public void setStoreType(StoreType storeType) {
		this.storeType = storeType;
	}

	/**
	 * session的redis配置
	 * @author hejian
	 *
	 */
	public static class Redis extends RedisProperties{

		/**
		 * Namespace for keys used to store sessions.
		 */
		private String namespace = "";

		/**
		 * Sessions flush mode.
		 */
		private RedisFlushMode flushMode = RedisFlushMode.ON_SAVE;

		public String getNamespace() {
			return this.namespace;
		}

		public void setNamespace(String namespace) {
			this.namespace = namespace;
		}

		public RedisFlushMode getFlushMode() {
			return this.flushMode;
		}

		public void setFlushMode(RedisFlushMode flushMode) {
			this.flushMode = flushMode;
		}

	}
}
